package com.wzy.java;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

public class test22 {

    /**
     * 使用反射给对象里设置值
     *
     * @param obj
     * @param clazz
     * @param filedName
     * @param typeClass
     * @param value
     */
    // 使用对象的set方法给对象赋值
    public static void setValue(Object obj, Class<?> clazz, String filedName, Class<?> typeClass, Object value) {
        String methodName = "set" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1);
        try {
            Method method = clazz.getDeclaredMethod(methodName, new Class[]{typeClass});
            method.invoke(obj, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取对象的get方法
     *
     * @param ob
     * @param name
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Object getGetMethod(Object ob, String name) throws InvocationTargetException, IllegalAccessException {
        String getName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);

        Method[] methods = ob.getClass().getMethods();
        for (Method method : methods) {
            String s = "get" + method.getName().substring(3, 4).toUpperCase() + method.getName().substring(4);
            if (getName.equals(s)) {
                System.out.println(s);
            }
        }
        return null;
    }

    /**
     * 得到属性值
     *
     * @param obj
     */
    public static String getObjValue(Object obj, Field field) {
        String value = null;
        try {
            field.setAccessible(true);
            value = field.get(obj).toString();

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return value;
    }


    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        Users users1 = new Users();
        System.out.println("getGetMethod:");
        getGetMethod(users1, "id");
        System.out.println("getGetMethod:");


        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 1; i <= 12; i++) {
            map.put(i, i * 3);
        }
        System.out.println(map);
        ArrayList<Integer> list = new ArrayList<>();
        list.add(11);
        list.add(13);
        list.add(14);
        list.add(16);
        list.add(18);
        list.add(20);
        list.add(22);
        list.add(24);
        list.add(26);
        list.add(28);
        list.add(30);
        list.add(100);
        Users users = new Users();

        // 方法一\
        Field[] fields = new Field[12];
        for (int i = 0; i < 12; i++) {
            try {
                Field field = Users.class.getDeclaredField("month" + (i + 1));
                fields[i] = field;
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        ArrayList<Users> list1 = new ArrayList<>();
        for (Field field : fields) {
            users.setId(1);
            users.setName("wzy");
            users.setAge1(23);
            field.setAccessible(true);
            for (int i = 1; i <= fields.length; i++) {
                if (field.getName().equals("month" + i)) {
                    try {
                        field.set(users, map.get(i));
                        String s = getObjValue(users, field);
                        System.out.println(field.getName() + ":" + s);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        list1.add(users);
        System.out.println("============================");
    }
}
